package com.hskn.hss.module.tlcarmodel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlcarmanufacturer.entity.TlCarManufacturer;
import com.hskn.hss.module.tlcarmanufacturer.service.ITlCarManufacturerService;
import com.hskn.hss.module.tlcarmodel.entity.TlCarModel;
import com.hskn.hss.module.tlcarmodel.mapper.TlCarModelMapper;
import com.hskn.hss.module.tlcarmodel.service.ITlCarModelService;
import com.hskn.hss.module.tlopetation.entity.TlOperation;
import com.hskn.hss.module.tlopetation.service.ITlOperationService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Service
public class TlCarModelServiceImpl extends ServiceImpl<TlCarModelMapper, TlCarModel> implements ITlCarModelService {

    @Resource
    TlCarModelMapper tlCarModelMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;
    @Resource
    ITlOperationService tlOperationService;
    @Resource
    ITlCarManufacturerService tlCarManufacturerService;


    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlCarModel tlCarModel = params.getObject("data", TlCarModel.class);

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlCarModel.setCreateby(uid);
        tlCarModel.setCreatetime(new Date());
        tlCarModel.setDeptid(deptid);
        if (tlCarModel.getStatus() == null || tlCarModel.getStatus().isEmpty()) {
            tlCarModel.setStatus("1");
        }
        tlCarModelMapper.insert(tlCarModel);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlCarModel tlCarModel = params.getObject("data", TlCarModel.class);
        tlCarModel.setUpdateby(uid);
        tlCarModel.setUpdatetime(new Date());
        tlCarModelMapper.updateById(tlCarModel);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlCarModelMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlCarModel getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlCarModel tlCarModel =  tlCarModelMapper.selectById(id);
        if (Objects.nonNull(tlCarModel)) {
            TlOperation tlOperation = tlOperationService.selectOne(tlCarModel.getOperationtypeid());
            if (Objects.nonNull(tlOperation)) {
                tlCarModel.setOperationtypename(tlOperation.getOperationtype());
            }
            TlCarManufacturer tlCarManufacturer = tlCarManufacturerService.selectOne(tlCarModel.getManufacturer());
            if (Objects.nonNull(tlCarManufacturer)) {
                tlCarModel.setManufacturername(tlCarManufacturer.getDicname());
            }
        }
        return tlCarModel;
    }

    @Override
    public List getSelectList(String cartypeid) {
        List resultList = tlCarModelMapper.getSelectList(cartypeid);
        return resultList;
    }

    @Override
    @DataScope(deptAlias = "deptid", userAlias = "createby, updateby")
    public JSONObject getList(BaseEntity entity, JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("operationtypeid", params.getString("operationtypeid"));
        map.put("cartypeid", params.getString("cartypeid"));
        map.put("carmodel", params.getString("carmodel"));
        map.put("manufacturer", params.getString("manufacturer"));
        map.put("status", params.getString("status"));
        map.put("drivemodel", params.getString("drivemodel"));
        map.put("params", entity.getParams());

        Page<TlCarModel> listPage = new Page<>(page, size, true);
        IPage<TlCarModel> iPage = tlCarModelMapper.listTlCarModel(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("resultlist", iPage.getRecords());
        return json;
    }
}
